//
//  HamatonBluetoothViewModel.swift
//  hamaton-app
//
//  Created by intyt on 2024/4/1.
//

import Foundation
import CoreBluetooth
import UIKit

enum HamatonLinkState {
    case  noLinking // 未连接
    case  linking // 连接中
    case  isLinking // 已连接
}

struct HamatonBluethoothCellModel {
    let peripheral: CBPeripheral
    let mac: String
    let state: HamatonLinkState
}

// 接收机设置-初始化数据使用
struct DeviceSettingCellModel {
    var carTypeModel: HamatonCarModel // 车辆类型
    var communicationTypeModel: HamatonCarModel // 通讯类型
    var CANCommunicationBaudRateModel: HamatonCarModel // CAN通讯波特率
    var CANTerminalResistanceModel: String // CAN终端电阻
    var CANMailingAddressModel: HamatonCarModel // CAN通讯地址
    var PCTRFChannel1Model: HamatonCarModel // RF通道1
    var PCTRFChannel2Model: HamatonCarModel // RF通道2
    var temperatureCompensationModel: String // 温度补偿
    var NumberOfTrailersModel: HamatonCarModel // 挂车数量
}

class DeviceData: NSObject {
    // 常规诊断
    var changgui: String?
    var changguiNum: String?
    // CAN诊断
    var CAN: String?
    var CANNum: String?
    // RF芯片诊断
    var RFXinpian: String?
    var RFXinpianNum: String?
    // UART诊断
    var UART: String?
    var UARTNum: String?
    // CAN芯片诊断
    var CANXinpian: String?
    var CANXinpianNum: String?
    // LPW诊断
    var LPW: String?
    var LPWNum: String?
}
// 常规诊断
var ChangguiCapitals = [
    "00": "无",
    "01": "ADC",
    "02": "ADV",
    "03": "IND",
    "04": "BOOT",
    "05": "NVM",
    "06": "SCAN",
    "07": "SDK",
    "08": "WDOG",
]
// CAN诊断
var CANCapitals = [
    "00": "无",
    "01": "失效",
    "02": "设置",
    "03": "初始化",
    "04": "复位",
    "05": "超时",
    "06": "发送",
    "07": "信息",
    "08": "忙"
]
// RF芯片诊断
var RFXinpianCapitals = [
    "00": "无",
    "01": "SPI读",
    "02": "SPI写",
    "03": "超时",
    "04": "SPI命令",
    "05": "配置",
    "06": "CRC",
    "07": "忙",
    "08": "溢出",
    "81": "芯片2",
    "82": "芯片2",
    "83": "芯片2",
    "84": "芯片2",
    "85": "芯片2"
]
// UART诊断
var UARTCapitals = [
    "00": "无",
    "06": "发送",
]

// CAN芯片诊断
var CANXinpianCapitals = [
    "00": "无",
    "20": "CAN芯片错误",
]

// LPW诊断
var LPWCapitals = [
    "00": "无"
]

// 接收机设置
// 通讯类型（列表里的字典顺序错乱，所以使用下边的CommunicationTypeList）
var CommunicationTypeOption = [
    "01": "BLE未过滤",
    "02": "CAN未过滤",
    "03": "RS232过滤（日本）",
    "04": "BLE过滤",
    "05": "CAN过滤EBS",
    "07": "RS232未过滤",
    "08": "CAN过滤",
    "09": "CAN车房",
    "0A": "CAN未过滤老的",
    "0C": "RS232过滤",
    "0E": "CAN RAW"
]
var CommunicationTypeOption2 = [
    "BLE未过滤": "01",
    "CAN未过滤": "02",
    "RS232过滤（日本）": "03",
    "BLE过滤": "04",
    "CAN过滤EBS": "05",
    "RS232未过滤": "07",
    "CAN过滤": "08",
    "CAN车房": "09",
    "CAN未过滤老的": "0A",
    "RS232过滤": "0C",
    "CAN RAW": "0E"
]
var CommunicationTypeList = [
    "1": "BLE未过滤",
    "2": "CAN未过滤",
    "3": "RS232过滤（日本）",
    "4": "BLE过滤",
    "5": "CAN过滤EBS",
    "6": "RS232未过滤",
    "7": "CAN过滤",
    "8": "CAN车房",
    "9": "CAN未过滤老的",
    "10": "RS232过滤",
    "11": "CAN RAW"
]
// 车辆类型
var CarTypeOption = [
    "00": "头车",
    "10": "挂车"
]
var CarTypeList = ["头车", "挂车"]
// RF通道1 NLP
var PCTRFChannel1Option = [
    "00": "OFF",
    "02": "VDA",
    "03": "NLP",
    "04": "PTG",
    "05": "PCT",
    "06": "WCO",
    "07": "HDS"
]
// RF通道2 HDS
var PCTRFChannel2Option = [
    "00": "OFF",
    "02": "VDA",
    "03": "NLP",
    "04": "PTG",
    "05": "PCT",
    "06": "WCO",
    "07": "HDS"
]
// CAN通讯波特率
var CANCommunicationBaudRateOption = [
    "00": "100K",
    "02": "250K",
    "03": "500K",
    "04": "800K"
]
// 开启关闭
var OnOffOption = [
    "00": "ON",
    "01": "OFF"
]
// 选择类型
enum SelectTypeEnum {
    case a
    case b
    case c
    case d
    case e
    case f
    case g
    case h
    case i
    case j
}
struct HamatonCarSettingCellModel {
    var hamatonCarModel: HamatonCarModel
}

struct HamatonCarModel {
    var id: String
    var name: String
}

class HamatonCarSettingViewModel {
    public var carTypeModel: HamatonCarModel? = nil // 车辆类型
    public var communicationTypeModel: HamatonCarModel? = nil // 通讯类型
    public var CANCommunicationBaudRateModel: HamatonCarModel? = nil // CAN通讯波特率
    public var CANTerminalResistanceModel: String = "01" // CAN终端电阻
    public var CANMailingAddressModel: HamatonCarModel? = nil // CAN通讯地址
    public var PCTRFChannel1Model: HamatonCarModel? = nil // RF通道1
    public var PCTRFChannel2Model: HamatonCarModel? = nil // RF通道2
    public var temperatureCompensationModel: String = "01" // 温度补偿
    public var NumberOfTrailersModel: HamatonCarModel? = nil // 挂车数量
    /// public var management: String = ""
    
     public func jundgeIsSave() -> String? {
         if carTypeModel == nil {
             return "请选择车辆类型"
         }
         if communicationTypeModel == nil {
             return "请选择通讯类型"
         }
         if CANCommunicationBaudRateModel == nil {
             return "请选择CAN通讯波特率"
         }
         if CANTerminalResistanceModel == nil {
             return "请选择CAN终端电阻"
         }
         if CANMailingAddressModel == nil {
             return "请选择CAN通讯地址"
         }
         if PCTRFChannel1Model == nil {
             return "请选择RF通道1"
         }
         if PCTRFChannel2Model == nil {
             return "请选择RF通道2"
         }
        return nil
    }
}
// 挂车mac
class trailerNumModel {
    public var macAddress1: String = ""
    public var macAddress2: String = ""
    public var macAddress3: String = ""
    public var macAddress4: String = ""
    public var macAddress5: String = ""
    public var macAddress6: String = ""
}

// 将字符串按两个一组用冒号隔开 如：11223344转为11:22:33:44
extension String {
    func groupCharactersByTwoWithColon1() -> String {
            var result = ""
            let characters = Array(self)
            
            for i in stride(from: 0, to: characters.count, by: 2) {
                let endIndex = min(i + 2, characters.count)
                let group = String(characters[i..<endIndex])
                result += group
                
                // 添加冒号，除非已经是最后一组
                if endIndex < characters.count {
                    result += ":"
                }
            }
            
            return result
        }
}

// 车辆轮位
class CarTyreModel {
    public var tyre16: Bool = false
    public var tyre17: Bool = false
    public var tyre19: Bool = false
    public var tyre1a: Bool = false
    public var tyre26: Bool = false
    public var tyre27: Bool = false
    public var tyre29: Bool = false
    public var tyre2a: Bool = false
    public var tyre36: Bool = false
    public var tyre37: Bool = false
    public var tyre39: Bool = false
    public var tyre3a: Bool = false
    public var tyre46: Bool = false
    public var tyre47: Bool = false
    public var tyre49: Bool = false
    public var tyre4a: Bool = false
}
// 阈值设置
class ThresholdSettingModel {
    public var diyaStr: String = ""
    public var zuidiyaStr: String = ""
    public var gaoyaStr: String = ""
    public var gaowenStr: String = ""
    public var shijianStr: String = ""
}
// 传感器类型
var CgqTypeOption = [
    "00": "未定义",
    "01": "保持在线",
    "02": "NLP",
    "03": "TMS",
    "04": "HLP",
    "05": "WU",
    "06": "EXTERNAL",
    "07": "PTG",
    "08": "TRAILER",
    "09": "WCO",
    "0A": "PROD",
    "0B": "SEN",
    "0C": "VMS",
    "0D": "WU_CFG"
]
var CgqTypeOption1 = [
    "0": "未定义",
    "1": "保持在线",
    "2": "NLP",
    "3": "TMS",
    "4": "HLP",
    "5": "WU",
    "6": "EXTERNAL",
    "7": "PTG",
    "8": "TRAILER",
    "9": "WCO",
    "10": "PROD",
    "11": "SEN",
    "12": "VMS",
    "13": "WU_CFG"
]
var CgqTypeOption2 = [
    "0": "00",
    "1": "01",
    "2": "02",
    "3": "03",
    "4": "04",
    "5": "05",
    "6": "06",
    "7": "07",
    "8": "08",
    "9": "09",
    "10": "0A",
    "11": "0B",
    "12": "0C",
    "13": "0D"
]

// 轮位传感器设置
class TyreSettingModel {
    public var luntaiweizhiStr: String = ""
    public var chuanganqiTypeStr: String = "" // 传感器类型name
    public var chuanganqiTypeId: String = "" // 传感器类型id
    public var chuanganqiIDStr: String = ""
    public var diyaStr: String = ""
    public var zuidiyaStr: String = ""
    public var gaoyaStr: String = ""
    public var gaowenStr: String = ""
    public var shijianStr: String = ""
}
// 轮胎传感器是否绑定
struct TyreTmps {
    var tyreNum: String
    var isBind: Bool
}
// 已绑定的轮胎数据
struct TyreTmpsInfo {
    var tyreNum: String
    var temp: String
    var pressure: String
}
//// 轮胎状态
//var TyreStatusOption = [
//    "0001": "FF69B4", // 低电量
//    "0002": "FF0000", // 低压
//    "0004": "FF0000", // 高压
//    "0008": "FF0000", // 高温
//    "0010": "FF0000", // 极低压
//    "0020": "DDDDDD", // 不详
//    "0040": "9400D3", // 错误生成
//    "0080": "FF0000", // 错误温度
//    "0100": "9400D3", // 错误移动
//    "0200": "FF0000", // 错误压力
//    "0400": "FF69B4", // 错误电压
//    "0800": "9400D3", // 传感器丢失
//    "UNKNown": "DDDDDD", // 未知
//]
// 轮胎状态
var TyreStatusOption = [
    "0001": UIColor(hex6: 0xFF69B4), // 低电量
    "0002": UIColor(hex6: 0xFF0000), // 低压
    "0004": UIColor(hex6: 0xFF0000), // 高压
    "0008": UIColor(hex6: 0xFF0000), // 高温
    "0010": UIColor(hex6: 0xFF0000), // 极低压
    "0020": UIColor(hex6: 0x9400D3), // 信号异常
    "0040": UIColor(hex6: 0x9400D3), // 错误生成
    "0080": UIColor(hex6: 0xFF0000), // 错误温度
    "0100": UIColor(hex6: 0x9400D3), // 错误移动
    "0200": UIColor(hex6: 0xFF0000), // 错误压力
    "0400": UIColor(hex6: 0xFF69B4), // 错误电压
    "0800": UIColor(hex6: 0x9400D3), // 传感器丢失
    "UNKNown": UIColor(hex6: 0xDDDDDD) // 未知
]

// 显示器设置
class XsqDisplaySettingModel {
    public var yalidanweiStr: String = ""  // 压力单位
    public var wendudanweiStr: String = "" // 温度单位
    public var xiumianshijianStr: String = "" // 休眠时间
    public var shengyinbaojingStr: String = "" // 声音报警
    public var guacheshuliangStr: String = "" // 挂车数量
    public var toucheMACStr: String = "" // 头车MAC
}
// 压力单位
var YalidanweiOption = [
    "00": "BAR",
    "01": "PSI"
]
// 温度单位
var WendudanweiOption = [
    "00": "°C",
    "01": "℉"
]
